package com.xmb.wcapi.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xmb.wcapi.constant.R;
import com.xmb.wcapi.entity.Inventory;
import com.xmb.wcapi.entity.StockNow;
import com.xmb.wcapi.entity.StockNowRecord;
import com.xmb.wcapi.exception.InterfaceErrorException;
import com.xmb.wcapi.mapper.InventoryMapper;
import com.xmb.wcapi.mapper.StockNowMapper;
import com.xmb.wcapi.mapper.StockNowRecordMapper;
import com.xmb.wcapi.service.IInventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmb.wcapi.service.IStockNowRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangping
 * @since 2024-04-12
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements IInventoryService {

    @Autowired
    private IStockNowRecordService recordService;

    @Autowired
    private StockNowMapper stockNowMapper;

    @Value("${startTime}")
    private String startTime;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reloadInvSum(String day) {
        //删除
        recordService.remove(Wrappers.lambdaUpdate(StockNowRecord.class).ge(StockNowRecord::getTimes,day + " 00:00:00"));
        //删除现存量
        stockNowMapper.delete(Wrappers.lambdaUpdate(StockNow.class));

        List<String> days = com.xmb.wcapi.util.DateUtil.getEveryDayToString(DateUtil.parse(day,"yyyy-MM-dd"), cn.hutool.core.date.DateUtil.yesterday());
        for (String toDay : days) {
            recordService.balanceDataNew(toDay);
        }
    }

}
